package com.cmas.platform.common.annotation;

import com.aidex.common.annotation.DataScope;
import com.aidex.common.annotation.DataSource;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.core.domain.BasePlusEntity;
import com.aidex.common.core.domain.entity.SysRole;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.model.LoginUser;
import com.aidex.common.enums.DataSourceType;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.platform.domain.Region;
import com.cmas.platform.service.RegionService;
import com.cmas.platform.service.impl.RegionServiceImpl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @Author su
 * @Date 2024/4/19 19:12
 */
@Aspect
@Component
public class OperatorDataScopeAspect {

    /**
     * 填充用户
     */
    public static final String TYPE_USER = "0";

    /**
     * 填充地区
     */
    public static final String TYPE_REGION = "1";

    @Autowired
    public RegionService regionService;


    @Before("@annotation(controllerDataScope)")
    public void doBefore(JoinPoint point, OperatorDataScope controllerDataScope) throws Throwable {
        handleDataScope(point, controllerDataScope);
    }

    protected void handleDataScope(final JoinPoint joinPoint, OperatorDataScope controllerDataScope) {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser)) {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()) {
                String value = "";
                if (TYPE_USER.equals(controllerDataScope.useType())) {
                    value = currentUser.getId();
                } else if (TYPE_REGION.equals(controllerDataScope.useType())) {
                    Region region = regionService
                            .getOne(new QueryWrapper<Region>().lambda().eq(Region::getUserId, currentUser.getId()));
                    if (StringUtils.isNotNull(region)) {
                        value = region.getId();
                    }
                }
                dataScopeFilter(joinPoint, controllerDataScope.fieldName(), value);

            }
        }
    }

    /**
     * 数据范围过滤
     *
     * @param joinPoint 切点
     * @param fieldName 字段名
     * @param value     值
     */
    public static void dataScopeFilter(JoinPoint joinPoint, String fieldName, String value) {
        Object param = joinPoint.getArgs()[0];
        if (StringUtils.isNotNull(param)) {
            try {
                Field field = param.getClass().getDeclaredField(fieldName);
                field.setAccessible(true);
                field.set(param, value);
            } catch (Exception e) {
                throw new CustomException(e.getMessage());
            }
        }
    }


}
